package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * DriverService的实现类，处理司机相关的业务逻辑
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    /**
     * Feign客户端，用于调用司机信息相关接口
     */
    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;

    /**
     * Redis模板，用于操作Redis缓存
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 司机登录方法
     *
     * @param code 司机登录的验证码或身份证明
     * @return 登录成功的令牌（Token）
     */
    @Override
    public String login(String code) {
        // 调用司机信息Feign客户端的登录方法，获取司机ID
        Result<Long> longResult = driverInfoFeignClient.login(code);
        Long driverId = longResult.getData();

        // 生成随机的Token
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        // 将司机ID与Token的映射关系存储到Redis中，并设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                driverId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);

        // 返回生成的Token
        return token;
    }

    /**
     * 获取司机登录信息
     *
     * @param driverId 司机ID
     * @return 司机登录信息对象
     */
    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        return driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
    }

    /**
     * 获取司机认证信息
     *
     * @param driverId 司机ID
     * @return 司机认证信息对象
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> authInfoVoResult= driverInfoFeignClient.getDriverAuthInfo(driverId);
        return authInfoVoResult.getData();
    }

    /**
     * 更新司机认证信息
     *
     * @param updateDriverAuthInfoForm 更新司机认证信息的表单
     * @return 更新操作的结果
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> booleanResult= driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm);
        Boolean data = booleanResult.getData();
        return data;
    }

    /**
     * 创建司机人脸模型
     *
     * @param driverFaceModelForm 司机人脸模型表单
     * @return 创建操作的结果
     */
    @Override
    public Boolean createDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult= driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);
        Boolean data = booleanResult.getData();
        return data;
    }

    /**
     * 检查司机是否进行了人脸识别
     *
     * @param driverId 司机ID
     * @return 人脸识别的结果
     */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    /**
     * 验证司机人脸
     *
     * @param driverFaceModelForm 司机人脸模型表单
     * @return 验证操作的结果
     */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }
    @Autowired
    private LocationFeignClient locationFeignClient;

    @Autowired
    private NewOrderFeignClient newOrderDispatchFeignClient;
    /**
     * 开始服务，进行司机认证和人脸识别，并更新服务状态
     *
     * @param driverId 司机ID
     * @return 服务开始的结果
     */
    @Override
    public Boolean startService(Long driverId) {
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
        if(driverLoginVo.getAuthStatus().intValue() != 2) {
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }
        Boolean isFaceRecognition = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if(!isFaceRecognition) {
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }
        driverInfoFeignClient.updateServiceStatus(driverId, 1);
        locationFeignClient.removeDriverLocation(driverId);
        newOrderDispatchFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

    @Override
    public Boolean stopService(Long driverId) {
        driverInfoFeignClient.updateServiceStatus(driverId, 0);
        locationFeignClient.removeDriverLocation(driverId);
        newOrderDispatchFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }
}
